import { setStorage } from '@/utils/storage'
import { removeToken } from '@/utils/cookies'
import { getMenu } from '@/api/menu'
import { constantRoutes } from '@/router'
import Message from '@/utils/message'
import Layout from '@/Layout/index.vue'
import Workbench from '../../views/workbench/index.vue'
import Much from '../../views/icons/much.vue'
// 匹配views里面所有的.vue文件
const modules = import.meta.glob('../../views/**/*.vue')
import { defineStore } from 'pinia'

export const usePermissionStore = defineStore('permission', {
  state: () => {
    return {
      addRoutes: [] as any[],
    }
  },
  actions: {
    // 获取子应用父级path
    // GetRouterPrefix() {
    //   return new Promise(async resolve => {
    //     let res = await getRouterPrefix()
    //     this.parentPath = res.data
    //     resolve(res.data)
    //   })
    // },
    // 获取路由数据
    generateRoutes() {
      return new Promise(resolve => {
        getMenu().then((res: any) => {
          
          let routerRes = res
          let routers = routerRes?.data
          // let newRouter = filterAsyncRouter(routers)
          // if (routerRes.data == null || newRouter.length === 0) {
          //   Message({
          //     message: '您没有该系统的访问权限，请联系管理员',
          //     type: 'warning',
          //     duration: 5 * 1000
          //   })
          //   removeToken()

          // }
          this.addRoutes = routers
          setStorage('routers', routers)
          resolve(routers)
        })
      })
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'permission',
        storage: localStorage,
      }
    ]
  }
})
